/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.prefs.Preferences;

/**
 * An implementation of AppPrefs that uses the java.util.prefs API.
 * 
 * 
 * @since May 30, 2009
 */

public class JUtilAppPrefs implements AppPrefs
{
  public static final String TIME_ZONE_IDS = "timeZoneIds";
  public static final String PATTERN = "pattern";
  public static final String ALWAYS_ON_TOP = "alwaysOnTop";
  public static final String TRANSLUCENT = "translucent";
  public static final String OPACITY_LEVEL = "opacityLevel";
 
  private Preferences prefs;
  
  public JUtilAppPrefs()
  {
    prefs = Preferences.userNodeForPackage(this.getClass());
  }

  /* (non-Javadoc)
   * @see org.tzconverter.AppPrefs#getDefaultPattern()
   */
  public String getPattern()
  {
    return prefs.get(PATTERN, AppPrefs.DEFAULT_PATTERN);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.AppPrefs#getDefaultTimeZoneIds()
   */
  public List<String> getTimeZoneIds()
  {
    String ids = prefs.get(TIME_ZONE_IDS, null);
    if(ids == null)
    {
      return AppPrefs.DEFAULT_IDs;
    }
    if("".equals(ids))
    {
      return new ArrayList<String>(0);
    }
    return Arrays.asList(ids.split(","));
  }

  /* (non-Javadoc)
   * @see org.tzconverter.AppPrefs#setDefaultPattern(java.lang.String)
   */
  public void setPattern(String pattern)
  {
    prefs.put(PATTERN, pattern);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.AppPrefs#setDefaultTimeZoneIds(java.util.List)
   */
  public void setTimeZoneIds(List<String> timeZoneIds)
  {
    StringBuilder sb = new StringBuilder();
    for(String id: timeZoneIds)
    {
      sb.append(id).append(",");
    }
    
    if(sb.length() > 0)
    {
      //Get rid of the extra comma
      sb.deleteCharAt(sb.length() - 1);
    }
    
    prefs.put(TIME_ZONE_IDS, sb.toString());
  }

  public boolean isAlwaysOnTop()
  {
    return prefs.getBoolean(ALWAYS_ON_TOP, AppPrefs.DEFAULT_ALWAYS_ON_TOP);
  }

  public void setAlwaysOnTop(boolean alwaysOnTop)
  {
    prefs.putBoolean(ALWAYS_ON_TOP, alwaysOnTop);
  }

  public boolean isTranslucent()
  {
    return prefs.getBoolean(TRANSLUCENT, AppPrefs.DEFAULT_TRANSLUCENT);
  }

  public void setTranslucent(boolean translucent)
  {
    prefs.putBoolean(TRANSLUCENT, translucent);
  }

  @Override
  public float getOpacityLevel()
  {
    return prefs.getFloat(OPACITY_LEVEL, AppPrefs.DEFAULT_OPACITY_LEVEL);
  }

  @Override
  public void setOpacityLevel(float level)
  {
   prefs.putFloat(OPACITY_LEVEL, level);    
  }
}
